import request from '../../../util/request'

import {
  HasSpuResponseData,
  AllTradeMarkResponseData,
  SpuImgResponseData,
  SpuSaleResponseData,
  BaseSaleAttrResponseData,
  SpuData,
  SkuData,
} from './type'

enum API {
  // 获取spu分页列表
  HASSPU_URL = '/admin/product/',
  // 获取全部SPU的品牌数据
  ALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  // 获取某个已有SPU下全部的商品图片地址
  SPUIMG_URL = '/admin/product/spuImageList/',
  // 获取某个SPU下的全部销售属性
  SPUSALE_URL = '/admin/product/spuSaleAttrList/',
  // 获取当前系统的所有销售列表
  ALLSALE_URL = '/admin/product/baseSaleAttrList',
  // 更新某个SPU
  UPDATE_URL = '/admin/product/updateSpuInfo',
  // 添加新的SPU
  ADD_URL = '/admin/product/saveSpuInfo',
  // 新增一个SKU信息
  ADD_SKU_URL = '/admin/product/saveSkuInfo',
  // 根据某个SPU的ID 查询改SPU下对应的SKU信息
  SELECT_SKU_URL = '/admin/product/findBySpuId/',
  // 删除某个SPU信息
  DELETE_SKU_URL = '/admin/product/deleteSpu/',
}

// 获取某个三级分类下已有spu列表数据
export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: string | number,
) => {
  return request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`,
  )
}

// 获取所有SPU品牌数据接口
export const reqAllTradeMark = () => {
  return request.get<any, AllTradeMarkResponseData>(API.ALLTRADEMARK_URL)
}

// 获取某个已有SPU下全部的商品图片地址
export const reqSpuImg = (id: number) => {
  return request.get<any, SpuImgResponseData>(API.SPUIMG_URL + `/${id}`)
}

// 获取某个SPU下的全部销售属性
export const reqSpuSaleAttr = (id: number) => {
  return request.get<any, SpuSaleResponseData>(API.SPUSALE_URL + `/${id}`)
}

// 获取当前系统的所有销售列表
export const reqAllSaleAttr = () => {
  return request.get<any, BaseSaleAttrResponseData>(API.ALLSALE_URL)
}

//更新已有的SPU接口
//data:即为新增的SPU|或者已有的SPU对象
export const reqAddOrUpdateSpu = (data: SpuData) => {
  //如果SPU对象拥有ID,更新已有的SPU
  if (data.id) {
    return request.post<any, any>(API.UPDATE_URL, data)
  } else {
    return request.post<any, any>(API.ADD_URL, data)
  }
}

// 给某个SPU下新增SKU信息
export const reqSaveSkuInfo = (data: SkuData) => {
  return request.post<any, any>(API.ADD_SKU_URL, data)
}

// 根据SPU的ID查询该SPU下对应的SKU信息
export const reqSelectSkuInfoById = (spuId: number) => {
  return request.get<any, any>(API.SELECT_SKU_URL + `${spuId}`)
}

// 删除某个SPU
export const reqDeleteSpu = (spuId: number) => {
  return request.delete<any, any>(API.DELETE_SKU_URL + `${spuId}`)
}
